from django.shortcuts import render

# Create your views here.
from django.views import View
from QQLoginTool.QQtool import OAuthQQ
from apps.oauth.models import OAuthQQUser
from meiduo_mall import settings
from django.http import JsonResponse, request
from django.contrib.auth import login
import json
from apps.users.models import User
from itsdangerous import URLSafeTimedSerializer
from apps.oauth.utils import generic_openid
from apps.oauth.utils import check_access
class QQLoginURLView(View):

    def get(self, request):
        # 1. 生成 QQLoginTool 实例对象
        # client_id=None,               appid
        # client_secret=None,           appsecret
        # redirect_uri=None,            用户同意登录之后，跳转的页面
        # state=None                    不知道什么意思，随便写。等出了问题再分析问题
        qq = OAuthQQ(client_id=settings.QQ_CLIENT_ID,
                     client_secret=settings.QQ_CLIENT_SECRET,
                     redirect_uri=settings.QQ_REDIRECT_URI,
                     state='xxxxx')
        # 2. 调用对象的方法生成跳转链接
        qq_login_url = qq.get_qq_url()
        # 3. 返回响应
        return JsonResponse({'code': 0, 'errmsg': 'ok', 'login_url': qq_login_url})

class OauthQQView(View):
    def get(self,requst):
        code= requst.GET.get('code')
        if code is None:
            return JsonResponse({'code':401,'errmsg':'参数不全'})
        
        qq = OAuthQQ(client_id=settings.QQ_CLIENT_ID,
                     client_secret=settings.QQ_CLIENT_SECRET,
                     redirect_uri=settings.QQ_REDIRECT_URI,
                     state='xxxxx')
        token = qq.get_access_token(code)
        openid = qq.get_open_id(token)
        try:
            qquser = OAuthQQUser.objects.get(openid=openid)
        except OAuthQQUser.DoesNotExist:
            
            access_token=generic_openid(openid)
            response = JsonResponse({'code': 402, 'access_token': access_token})
            return response
        else:
            # 存在
            # 6. 如果绑定过，则直接登录
            # 6.1 设置session
            login(request, qquser.user)
            # 6.2 设置cookie
            response = JsonResponse({'code': 0, 'errmsg': 'ok'})

            response.set_cookie('username', qquser.user.username)

            return response

    def post(self, request):
        # 1. 接收请求
        data = json.loads(request.body.decode())

        mobile=data.get('mobile')
        password=data.get('password')
        sms_code=data.get('sms_code')
        access_token=data.get('access_token')
        openid=check_access(access_token)
        if openid is None:
            return JsonResponse({'code':400,'errmsg':'参数缺失'})
        #校验忽略
        try:
            user=User.objects.get(mobile=mobile)
        except User.DoesNotExist:
            #手机号不存在，创建一个用户，用户名=手机号
            user = User.objects.create_user(username=mobile,mobile=mobile,password=password)
        else:
            #手机号存在，判断密码是否正确
            if  not user.check_password(password):
                return JsonResponse({'code':400,'errmsg':'账号或密码不正确'})
        OAuthQQUser.objects.create(user=user,openid=openid)
        login(request,user)
        response= JsonResponse({'code':0,'errmsg':'绑定成功'})
        response.set_cookie('username',user.username)
        return response


